package com.aaa.controller.staff;

import com.aaa.entity.ResponseDto;
import com.aaa.service.StaffService;
import com.aaa.service.impl.StaffServiceImpl;
import com.aaa.util.IntegerUtils;
import com.google.gson.Gson;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@WebServlet("/FindAllStaffServlet")
public class FindAllStaffServlet extends HttpServlet {
    StaffService staffService;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {




        ResponseDto responseDto = new ResponseDto();
        try {

            Integer pageNumber = IntegerUtils.ToInteger(req.getParameter("pageNumber"));
            Integer pageSize = IntegerUtils.ToInteger(req.getParameter("pageSize"));
            String searchStaffId = req.getParameter("searchStaffId");
            String searchName = req.getParameter("searchName");
            Map<String,Object> map = staffService.findAllStaff(pageNumber,pageSize,searchStaffId,searchName);
            responseDto.setStatus(ResponseDto.SUCCESS_CODE);
            responseDto.setData(map);
            responseDto.setMessage("操作成功");
        }catch (Exception e){
            responseDto.setStatus(ResponseDto.FAILURE_CODE);
            responseDto.setMessage("操作失败");
        }
        resp.getWriter().write(new Gson().toJson(responseDto));

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       doGet(req, resp);
    }

    @Override
    public void init(ServletConfig config) throws ServletException {
        staffService = new StaffServiceImpl();
    }
}
